"""
京东手机按销量排行
https://search.jd.com/Search?keyword=%E6%89%8B%E6%9C%BA&wq=%E6%89%8B%E6%9C%BA&pvid=8858151673f941e9b1a4d2c7214b2b52&psort=3&click=0
Referer、User-Agent、Cookie
"""
import requests
from pyquery import PyQuery as pq

phone_list = []
headers = {
    "Referer": "https://www.jd.com/",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36",
    "Cookie": "shshshfpa=52bbad0e-8ce0-b3cb-054b-13c5373f494a-1711760009; shshshfpx=52bbad0e-8ce0-b3cb-054b-13c5373f494a-1711760009; __jdu=1711760009234918891092; areaId=7; ipLoc-djd=7-412-416-47178; TrackID=1n545JslfLPXSSREsIKsRe2ziofKoeyu98jdy3KzjKvMpmEpJx6noc0E9mYMb0ZkeOyV550SqZ9hFn5QFHGjESnm6VKezT4js-zZOPPiRyWs; thor=D9DDA39C03E46ABC2925DDCF86A69446D5F1AF8B3E939FA74A534F57265F63C0086BF9A0B60C3E12B38487825694BE546063F0D32D52D58261FA6783586FE92CC87EC706EE585F5F41C9EBBCABCF983E63F56DFEA4BD164F291EE7F3068B08803EC90EDF006D5B0C7C3132B9A3FE43F024F69BDB53559BBE4928EE19BCD2F6A7A83218BA78702971F98FA2595B760E1BD4F671864ACDE7BB08A94A1A2FC16459; pinId=9iJXR6K4lvg9VmK5oxMGaQ; pin=jd_cVFwBgjlScfv; unick=jd_cVFwBgjlScfv; _tp=6nVNNbA23%2FPFBvK6%2FAVIDg%3D%3D; _pst=jd_cVFwBgjlScfv; flash=2_WQBDdR7iFHqkK378BR0ZzMPZRqI1AlRgx8U3u_fSOU4mk8LCs1cseqJsJgz2Wuwfxf-QuBK2FEvxk1H_DuFj4bT-cyylEWrGRuf4Iax10ClyNVZEaBVH4f7yZMTCJyUQmm3EgmHEywqA9IeYD6mcBT6sa1FLnotz2dbZEkAA40j*; unpl=JF8EALJnNSttUUgEBxlXEhRDH1pTWwpdTkdTO2MAVF8PTVVRTwFOEhV7XlVdWBRKEx9vYBRXXFNPVg4aBisSEXteU11bD00VB2xXXAQDGhUQR09SWEBJJVpUW1gIQhUDbW4MZG1bS2QFGjIbFBBLW1NfXAlDFAFqbwVdVFBLVQ0YMhoiF3ttZFZcDEkWAF9mNVVtGh8IAxMDExQVBl1SXl0OTBYCbm8GVlhQS10MEwIaGhN7XGRd; __jdv=76161171|haosou-search|t_262767352_haosousearch|cpc|61441831388_0_82a33d87be664fd4aae5413f70ee2d14|1715863985420; PCSYCityID=CN_410000_410100_0; qrsc=3; 3AB9D23F7A4B3CSS=jdd037E5SZXW3ZX5KEZUX6RILGWEGCKRSHF2SKMIQPMYUVFAYVFVV4KVYRWWEEPJTSOEL6H3FYRLTDHXFW6S25ZQUL7POVMAAAAMPQQHETCIAAAAADF5WVB4H724B3YX; _gia_d=1; jsavif=1; jsavif=1; xapieid=jdd037E5SZXW3ZX5KEZUX6RILGWEGCKRSHF2SKMIQPMYUVFAYVFVV4KVYRWWEEPJTSOEL6H3FYRLTDHXFW6S25ZQUL7POVMAAAAMPQQHETCIAAAAADF5WVB4H724B3YX; __jda=143920055.1711760009234918891092.1711760009.1715867631.1715907478.17; __jdb=143920055.2.1711760009234918891092|17.1715907478; __jdc=143920055; rkv=1.0; 3AB9D23F7A4B3C9B=7E5SZXW3ZX5KEZUX6RILGWEGCKRSHF2SKMIQPMYUVFAYVFVV4KVYRWWEEPJTSOEL6H3FYRLTDHXFW6S25ZQUL7POVM; shshshfpb=BApXc-vAGh-pATQMefqddf7eCerKM-fiQBlM3ByZj9xJ1MjdZGoC2"
}
url = "https://search.jd.com/Search?keyword=%E6%89%8B%E6%9C%BA&wq=%E6%89%8B%E6%9C%BA&pvid=8858151673f941e9b1a4d2c7214b2b52&psort=3&click=0"
res = requests.get(url, headers=headers)
# print(res.content.decode())
query = pq(res.content.decode())
datas = query(".gl-item div")
# print(datas)
for data in datas:
    data = pq(data)
    link = data("p-img a").attr("href")
    main_image = data(".p-img img").attr("data-lazy-img")
    color_list = []
    colors = data(".p-scroll .ps-wrap .ps-main .ps-item")
    for color in colors:
        color = pq(color)
        color_name = color("a").attr("title")
        color_list.append(color_name)
    price = data(".p-price strong i").text()
    name = data(".p-name a em").text()
    shop_link = data(".p-shop span a").attr("href")
    shop = data(".p-shop span a").text()
    coupon = data(".p-icons i").text()
    phone_list.append({
        "main_image": main_image,
        "colors": color_list,
        "name": name,
        "link": link,
        "price": price,
        "shop": shop,
        "shop_link": shop_link,
        "coupon": coupon
    })
    print(name, price, main_image, color_list, link, shop, shop_link, coupon)

# for phone in phone_list:
#     print(phone)